package Enum2;

import java.lang.reflect.Constructor;
import java.lang.reflect.InvocationTargetException;
import java.security.SecureRandom;

public class Test {
    //枚举的构造方法只能是私有的，能否通过反射，拿到实例对象呢？不能
    public static void reflectPrivateConstructor() {
        try {
            Class<?> c1 = Class.forName("Enum2.TestEnum");

            Constructor<?> constructor = c1.getDeclaredConstructor(String.class,int.class,Double.class, String.class);

            constructor.setAccessible(true);

            TestEnum testEnum = (TestEnum) constructor.newInstance("123",6,9.9,"蓝色");


            System.out.println(testEnum);

        } catch (ClassNotFoundException e) {
            throw new RuntimeException(e);
        } catch (NoSuchMethodException e) {
            throw new RuntimeException(e);
        } catch (InvocationTargetException e) {
            throw new RuntimeException(e);
        } catch (InstantiationException e) {
            throw new RuntimeException(e);
        } catch (IllegalAccessException e) {
            throw new RuntimeException(e);
        }

    }

    public static void main(String[] args) {
        reflectPrivateConstructor();
    }
}


